#include "CardType.h"
#include "CardTypeData.h"
#include "CardTypeManager.h"
#include "PokerData.h"
#include <iostream>

OnePair::OnePair() {
}

OnePair::~OnePair() {
}

CardTypeData OnePair::getCardType(const PokerData & data) const {
  vector<Poker> pokers = data.getPokers();
  int count = pokers.size();
  bool one = false;
  int value = 0;
  if (count > 4) {
    PokerData::sortData(pokers);
    for (int i = 0; i < count - 1; i++) {
      if (pokers.at(i).value == pokers.at(i+1).value) {
        one = true;
        value = pokers.at(i).value;
        break;
      }
    }
    if (one) {
      vector<Poker> other;
      for (int i = 0; i < count; i++) {
        const Poker & p = pokers.at(i);
        if (p.value != value)
          other.push_back(p);
      }

      CardTypeData cardTypeData;
      cardTypeData.type = CardType::OnePair;
      cardTypeData.first = value;
      cardTypeData.second = other.at(0).value;
      cardTypeData.third = other.at(1).value;
      cardTypeData.fourth = other.at(2).value;
      return cardTypeData;
    }
  }
  return getNextType()->getCardType(data);
}

const CardType * OnePair::getNextType() const {
  return CardTypeManager::sharedCardTypeManager()->getCardType(CardType::HighCard);
}
